package view.Windows;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import view.Items.ReviewItemGUI;

import commons.Entities.LegenCharacter;
import commons.Entities.Review;
import commons.Entities.User;

import controller.ControllerFunctions;

public class ReviewPageGUI {

	final protected Shell shlMain;
	protected Color cBackgroundColor;
	protected Font fHeadFont;
	protected Font fLabelFont;
	protected Font fButtonFont;
	protected DateTime calendar;
	protected Text text;
	protected ReviewItemGUI[] riArr = new ReviewItemGUI[20];

	public ReviewPageGUI(Display mainDisp, final User user, final LegenCharacter character) {

		initFonts(mainDisp);

		shlMain = new Shell(mainDisp, SWT.SHELL_TRIM & ~SWT.RESIZE & SWT.CLOSE);
		shlMain.setBounds(10, 10, 600, 700);

		// locate the shell in the center of the display
		Monitor primary = mainDisp.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shlMain.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shlMain.setLocation(x, y);

		// main definitions
		shlMain.setText("LegenDate - Add review for " + character.getCharacterName());
		Image icon = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDate.png"));
		shlMain.setImage(icon);
		Color clrWhite = new Color(mainDisp, 255, 255, 255);
		Image imgBackground = new Image(mainDisp, getClass().getClassLoader().getResourceAsStream("view/Assets/LegenDateBG2.jpg"));
		shlMain.setBackgroundImage(imgBackground);
		shlMain.setBackgroundMode(SWT.INHERIT_FORCE);

		// header
		Label lblReviewHeader = new Label(shlMain, SWT.CENTER);
		lblReviewHeader.setText("Review for " + character.getCharacterName());
		lblReviewHeader.setBounds(10, 30, 580, 60);
		lblReviewHeader.setForeground(clrWhite);
		lblReviewHeader.setFont(getHeadFont());

		// the review pane
		ScrolledComposite scResults = new ScrolledComposite(shlMain, SWT.V_SCROLL | SWT.BORDER);
		scResults.setBounds(-2, 100, 600, 500);
		scResults.setExpandVertical(true);
		final Composite cResults = new Composite(scResults, 0);
		cResults.setBounds(0, 0, 600, 1010);
		final ScrollBar sb = scResults.getVerticalBar();
		sb.setVisible(true);
		sb.addListener(SWT.Selection, new Listener() {

			public void handleEvent(Event e) {
				Point location = cResults.getLocation();
				location.y = -sb.getSelection() * (600 / sb.getMaximum());
				cResults.setLocation(location);
			}
		});

		for (int i = 0; i < 20; i++) {
			riArr[i] = new ReviewItemGUI(Review.getBadSide()[i], Review.getGoodSide()[i], 50, 10 + 50 * i, cResults, mainDisp);
		}

		// add review button
		Button btnAddReview = new Button(shlMain, SWT.NONE);
		btnAddReview.setFont(getButtonFont());
		btnAddReview.setText("Add Review!");
		btnAddReview.setBounds(200, 615, 200, 30);
		btnAddReview.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				Review review = new Review();
				review.setCharacterID(character.getCharacterID());
				review.setAffectionateORcold(riArr[0].getRate());
				review.setCheerfulORgrumpy(riArr[1].getRate());
				review.setConsiderateORinconsiderate(riArr[2].getRate());
				review.setCourteousORrude(riArr[3].getRate());
				review.setGenerousORstingy(riArr[4].getRate());
				review.setFriendlyORhostile(riArr[5].getRate());
				review.setHonestORdishonest(riArr[6].getRate());
				review.setHumbleORarrogant(riArr[7].getRate());
				review.setKindORcruel(riArr[8].getRate());
				review.setMatureORimmature(riArr[9].getRate());
				review.setSelfConfidentORinsecure(riArr[10].getRate());
				review.setSelflessORselfish(riArr[11].getRate());
				review.setSensitiveORInsensitive(riArr[12].getRate());
				review.setSeriousORsilly(riArr[13].getRate());
				review.setCleanORdirty(riArr[14].getRate());
				review.setGoodORevil(riArr[15].getRate());
				review.setGoodLookingORugly(riArr[16].getRate());
				review.setGracefulORclumsy(riArr[17].getRate());
				review.setIntelligentORstupid(riArr[18].getRate());
				review.setWealthyORpoor(riArr[19].getRate());

				ControllerFunctions.get().addNewCharacterReview(review);
				shlMain.close();
				shlMain.dispose();
			}
		});

		// close popup when pressing 'escape'
		shlMain.addListener(SWT.Traverse, new Listener() {

			public void handleEvent(Event event) {
				switch (event.detail) {
					case SWT.TRAVERSE_ESCAPE:
						shlMain.close();
						shlMain.dispose();
				}
			}
		});

	}

	public Color getBackgroundColor() {
		return cBackgroundColor;
	}

	public void setBackgroundColor(Color cBackgroundColor) {
		this.cBackgroundColor = cBackgroundColor;
	}

	public Font getHeadFont() {
		return fHeadFont;
	}

	public void setHeadFont(Font fHeadFont) {
		this.fHeadFont = fHeadFont;
	}

	public Font getLabelFont() {
		return fLabelFont;
	}

	public void setLabelFont(Font fLabelFont) {
		this.fLabelFont = fLabelFont;
	}

	public Font getButtonFont() {
		return fButtonFont;
	}

	public void setButtonFont(Font fButtonFont) {
		this.fButtonFont = fButtonFont;
	}

	/**
	 * initialize fonts
	 * 
	 * @param display
	 */
	protected void initFonts(Display display) {
		setHeadFont(new Font(display, new FontData("Fiolex Girls", 20, SWT.BOLD)));
		setLabelFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));
		setButtonFont(new Font(display, new FontData("Ariel", 10, SWT.BOLD)));

	}

	/**
	 * show the shell
	 * 
	 * @param display
	 */
	public void showGUI(Display display) {
		shlMain.open();
		// this is what's holding the shell open!!!
		while (!shlMain.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

}
